import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import { useCallback, useMemo } from 'react';
import { useRequest } from 'umi';
import { DelegateManageAPI } from '../common/type';
import { getSearchColumns } from '../mconfig';

let baseUrl = getBaseUrl();
const delegateTableManage = (curRes: Record<string, any>) => {
  /**
   * 获取列表数据
   */
  const { tableProps, data, error, loading, run, params } = useRequest(
    (opt: DelegateManageAPI.IGridRequestType) => {
      let personUrl = `${baseUrl}/activitiController/delegateDataGrid`;
      let personBody: any = {
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        ...opt,
      };
      return requestFunComponFiex('POST', personUrl, personBody);
    },
    {
      manual: true, //页面初始请求为true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getDelegateTabledData = useCallback(
    (otherRequest: DelegateManageAPI.IGridRequestType | any) => {
      const request: DelegateManageAPI.IGridRequestType | any = {
        ...otherRequest,
      };

      if (request?.createTime) {
        const [startTime, endTime] = request.createTime;
        request['createStartTime'] = startTime.format('YYYY-MM-DD HH:mm:ss');
        request['createEndTime'] = endTime.format('YYYY-MM-DD HH:mm:ss');
        delete request['createTime'];
      }

      if (request?.modifyTime) {
        const [startTime, endTime] = request.modifyTime;
        request['modifyStartTime'] = startTime.format('YYYY-MM-DD HH:mm:ss');
        request['modifyEndTime'] = endTime.format('YYYY-MM-DD HH:mm:ss');
        delete request['modifyTime'];
      }

      // if (request?.ownerFormNameDept) {
      // delete request['ownerFormNameDept'];
      // delete request['ownerFormNameUser'];
      // }

      // if (request?.ownerID && request?.ownerID?.length > 0) {
      //   request['ownerID'] = request?.ownerID?.toString();
      // } else {
      //   delete request['ownerID'];
      // }

      run({
        ...request,
      });
    },
    [],
  );

  /* 页面列表 */
  let tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /* 数据字典值  */
  let loadMapList = useMemo(() => {
    return {
      searchList: getSearchColumns([], params),
    };
  }, [params]);

  return {
    loading,
    getDelegateTabledData, //其他请求操作方法例如刷新等等
    // manageTableParams,
    otherParams: params,
    ...tableValues, //页面列表值
    ...loadMapList,
  };
};

export { delegateTableManage };
